import React, { useState, useEffect } from 'react'
import { Form, TreeSelect, message } from 'antd'
import { getTreeData } from '../ajax'
import { awaitWrap } from '@js/tool'
import { TreeData } from '@src/components/schema/tree'
const { Item } = Form

// 初始化案由树数据
async function initTreeData (setTreeData: (v: TreeData[]) => void) {
  const [e, d] = await awaitWrap(getTreeData())
  if (e === null && d !== null) {
    setTreeData(d)
  }
}

function Main () {
  const [treeData, setTreeData] = useState<TreeData[]>([])
  useEffect(() => {
    initTreeData(setTreeData)
  }, [setTreeData])
  return (
    <Item name='cause_action' label='案由'>
      <TreeSelect
        style={{ width: '100%' }}
        dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
        treeData={treeData}
        placeholder='请选择案由'
        maxTagTextLength={10}
        multiple
        showSearch
      />
    </Item>
  )
}

export default Main
